package cn.faurecia.whstock.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.faurecia.dto.InStockRo;
import cn.faurecia.dto.StockVo;
import cn.faurecia.dto.WhInStock;
import cn.faurecia.dto.WhItem;
import cn.faurecia.dto.WhStock;
import cn.faurecia.whinstock.service.impl.WhInstockServiceImpl;
import cn.faurecia.whitem.dao.WhItemMapper;
import cn.faurecia.whitem.service.impl.WhItemServiceImpl;
import cn.faurecia.whstock.dao.WhStockMapper;
import cn.faurecia.whstock.service.IwhStockService;


@Service("whStockService")
public class WhStockServiceImpl implements IwhStockService {
  @Autowired
  private WhItemMapper itemDao;
  @Autowired
  private WhStockMapper stockDao;
  @Autowired
  private WhItemServiceImpl itemService;
  @Autowired
  private WhInstockServiceImpl instockService;

  @Override
  public List<WhStock> queryByParam(WhStock stock) {
    return stockDao.queryByParam(stock);
  }

  @Override
  public Map<String, Object> instock(WhItem item,String code0,String code1,String code2,String code3,String stockId) {
    WhStock stock = new WhStock();
    stock.setItemId(item.getId());
    List<WhStock> dataList = stockDao.queryByParam(stock);
    Map<String, Object> map = new HashMap<String, Object>();
    if (dataList.isEmpty()) {
      map.put("result","1");
      map.put("msg","入库失败");
      return map;
    }else {
      WhStock dto = dataList.get(0);
      dto.setIntimeCnt(dto.getIntimeCnt() + 4);
      dto.setCompCnt(dto.getCompCnt() + 4);
      dto.setWaitCnt(dto.getWaitCnt() -4);
      stockDao.updateByPrimaryKey(dto);
      
      WhInStock inStock = new WhInStock();
      inStock.setItemCode(code0);
      inStock.setItemId(item.getId());
      inStock.setStock(stockId);
      instockService.insert(inStock);
      
      inStock.setItemCode(code1);
      instockService.insert(inStock);
      
      inStock.setItemCode(code2);
      instockService.insert(inStock);
      
      inStock.setItemCode(code3);
      instockService.insert(inStock);
      map.put("result","0");
      map.put("msg","入库成功");
      return map;
    }
  }

  @Override
  public WhStock getStockByItem(int itemId) {
    WhStock stock = new WhStock();
    stock.setItemId(itemId);
    List<WhStock> dataList = stockDao.queryByParam(stock);
    if (dataList.isEmpty()) {
      return null;
    }else {
      return dataList.get(0);
    }
  }

  @Override
  public int updateByPrimaryKey(WhStock record) {
    return stockDao.updateByPrimaryKey(record);
  }

  @Override
  public Map<String, Object> outstock(String[] itemCodes) {
    Map<String, Object> map = new HashMap<String, Object>();
    for (String code : itemCodes) {
      WhItem item = itemService.getItemByCode(code);
      if (item == null) {
        map.put("result", 1);
        map.put("msg", "出库失败");
        return map;
      }else {
        WhStock stock = getStockByItem(item.getId());
        if (stock == null) {
          map.put("result", 1);
          map.put("msg", "出库失败");
          return map;
        }else {
          int intimeCnt = stock.getIntimeCnt();
          if (intimeCnt < 4) {
            map.put("result", 2);
            map.put("msg", "库存不足");
            return map;
          }
          stock.setIntimeCnt(intimeCnt-4);
          stockDao.updateByPrimaryKey(stock);
        }
      }
    }
    map.put("result", 0);
    return map;
  }

  @Override
  public List<StockVo> queryFullData() {
    return stockDao.queryFullData();
  }
  
  
}
